#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
#include "./base_CGI.h"

void Create_info_Header() 
{
  printf(
      "<html>"
      "<head>"
      "<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">"
      "<title>投递岗位详情</title>"
      "<style type=\"text/css\">"
      "body{"
      "BACKGROUND-COLOR: #c0c0c0; MARGIN: 0px}"
      "</style>"
      "</head>"
      "<body>"
      "<TABLE border=\"0\" cellSpacing=\"1\" cellPadding=\"2\" width=\"95%%\" align=\"center\">"
      "<COLGROUP>"
      "<COL width=\"40\">"
      "<COL width=\"150\">"
      "<COL width=\"78\">"
      "<COL width=\"162\">"
      "<COL width=\"40\">"
      "<COL width=\"54\">"
      "<COL width=\"68\">"
      "</COLGROUP>"
      "<br>"
      "<h2 align=\"center\">投递岗位情况</h2>"
      "<TBODY>"
      "<TR height=\"22\" bgColor=\"#ccccff\">"
      "<TH height=\"39\" rowSpan=\"2\" width=\"40\">序号</TH>"
      "<TH rowSpan=\"2\" width=\"180\">公司名称</TH>"
      "<TH rowSpan=\"2\" width=\"145\">岗位名称</TH>"
      "<TH rowSpan=\"2\" width=\"108\">时间</TH>"
      "<TH rowSpan=\"2\" width=\"162\">状态</TH>"
      "</TR>"
      "<TR height=\"20\"></TR>"
      );
}


void Create_info_Tital()
{
  printf("</TBODY>"
      "</TABLE>"
      "</body>"
      "</html>");
}

int  Process_info(MYSQL *connect_fd,const char * name)
{
  char sql[1024 * 5] = {0};
  sprintf(sql,"select * from info where user_name = '%s'",name);
  fprintf(stderr , "hhehhhhh:%s",sql);
  int ret = mysql_query(connect_fd, sql);
  if(ret < 0)
  {
    fprintf(stderr, "mysql_query failed!\n");
    return 1;
  }
  MYSQL_RES * result = mysql_store_result(connect_fd);
  if(result == NULL)
  {
      fprintf(stderr,"mysql_store_result failed!\n");
      return 1;
    }
    //构造返回的请求
   Create_info_Header(); 

  
//   <TR height="34" bgColor="#ffffff">
//     <TD height="34" width="40">7</TD>
//     <TD width="180">公司金融</TD>
//     <TD width="145" noWrap>金融学院</TD>
//     <TD width="108">王国红</TD>
//     <TD width="162">校级精品课程</TD>
//    </TR>

    int rows = mysql_num_rows(result);
    int fields = mysql_num_fields(result);
    //b)把表内容获取到
    int i = 0 ;
    fprintf(stderr, "\nrows: %d   fields :%d\n",rows,fields);
    for(i = 0 ; i < rows; ++i)
    {
      MYSQL_ROW row = mysql_fetch_row(result);
      printf("<TR height=\"34\" bgColor=\"#ffffff\">");
      printf("<TD width=\"34\" width=\"40\">%d</TD>",i);
      printf("<TD width=\"180\" >%s</TD>",row[1]);
      printf("<TD width=\"145\"noWrap >%s</TD>",row[2]);
      printf("<TD width=\"108\">%s</TD>",row[3]);
      printf("<TD width=\"162\">%s</TD>",row[4]);
      printf("</TR>");

      fprintf(stderr, "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
      fprintf(stderr, row[1]);
      fprintf(stderr, row[4]);
    }
   Create_info_Tital();
  return 0;  
}



int main()
{
  //获取参数
  char buf[1024 * 4] = {0};
  if(Get_info(buf) < 0)
  {
    fprintf(stderr, "Get_info failed!\n");
    return 1;
  }
  fprintf(stderr, "Get_info OK\n");

  char name[1024] = {0};
  char password[1024] = {0};
  char * p = buf;
  p += strlen("name=");
  int i = 0;
  while(*p != '&' && *p != '\0')
  {
    name[i++] = *p;
    p++;
  }
  name[i++] = '\0';
  p += strlen("password=");
  p++;
  i = 0;
  while(*p != '&' && *p != '\0')
  {
    password[i++] = *p;
    p++;
  }
  password[i] = '\0';


  fprintf(stderr, "name : %s   password :%s\n",name, password);

  //1.初始化 mysql 句柄
  MYSQL * connect_fd = mysql_init(NULL);
  //2.建立连接
     //参数含义(参数为什么这么多)：
     //a)mysql 连接句柄
     //b)mysql 服务器的ip地址
     //c)用户名
     //d)密码
     //e)要访问的数据库名
     //f)端口号
     //g)NULL
     //h)标志位
  if(mysql_real_connect(connect_fd, "127.0.0.1", "root", "1", "ForOffer", 3306, NULL, 0) == NULL)
  {
    fprintf(stderr, "mysql_real_connect failed!\n");
    return 1;
  }
  if ( mysql_set_character_set(connect_fd, "utf8"  )  ) { 
              fprintf ( stderr , "错误, %s\n" , mysql_error( connect_fd)  ) ; 
          } 
  
  //从数据库中查找所有用户是否存在，不存在则返回404，存在就将该用户的信息返回
  
  const char * sql = "select * from user";
  //4.把sql语句发送到服务器上
  int ret = mysql_query(connect_fd, sql);
  if(ret < 0)
  {
    fprintf(stderr, "mysql_query failed!\n");
    return 1;
  }
  //5.遍历结果集（服务器响应的表结构）
    //按行遍历，每次取出一行，再依次把所有的列都取出来
    MYSQL_RES * result = mysql_store_result(connect_fd);
    if(result == NULL)
    {
      fprintf(stderr,"mysql_store_result failed!\n");
      return 1;
    }
    //a)先获取到几行，以及几列
    int rows = mysql_num_rows(result);
    int fields = mysql_num_fields(result);
    //b)把表内容获取到
    i = 0 ;

    fprintf(stderr, "\nrows: %d   fields :%d\n",rows,fields);
    for(i = 0 ; i < rows; ++i)
    {
      MYSQL_ROW row = mysql_fetch_row(result);
        int name_1 = strcmp(name, row[1]);
        int password_2 = strcmp(password, row[2]);
        
        fprintf(stderr,"ret_1 :%d\n",name_1);
        fprintf(stderr,"ret_2 :%d\n",password_2);
        if(name_1 == 0 && password_2 == 0)
        {
          goto Flag;
        }
    }

Flag:
    if(i < rows)//说明在数据库中查找到了该用户
    {
      //从数据库中将该用户的信息都查找出来
      //处理将该用户的信息以表格的形式返回
      
      //查找该用户的信息，并且以表格的形式给客户返回
      
      Process_info(connect_fd,name);
     // printf("<html><head><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">"

     // " </head><body>404 Not Found\n, 找到该用户了。页面错误\n</body></html>");
    }
    else
    {
      printf("<html><head><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">"
      " </head><body>404 Not Found\n,没找到用户。页面错误\n</body></html>");
      //返回404页面
    }

  //5.断开连接（*****不能忘*****）
  mysql_close(connect_fd);
  return 0;
}
